<?php

myauth();
if(!$action)$action='list';
$active['raid'] = 'active';

if($raidid)
{
    $Raid = new Raid($raidid);
    $raid = $Raid->info;
}

if($action=='list')
{
    $cond = "";
    if($raid)$cond .= " and r_id='$raid[r_id]' ";
    if($dkpid)$cond .= " and dkp_id='$dkpid' ";
    $sql = "select * from raids where 1 $cond order by r_id desc";
    $pager = new ShowPage($db1, 25, $sql);
    $sql = $pager->GetNewSql();
    $rs = $db1->query($sql);
    //$n = 1;
    while($row = $rs->fetch())
    {
        //if($n%2==0)$row['css'] = 'odd';
        $row['icon'] = $dkps[$row[dkp_id]][d_icon];
        $rows[] = $row;
    }


    include template('admin/raid');
}


if($action=='info')
{
    

    if($_SERVER['REQUEST_METHOD']=='POST')
    {
        $id = $Raid->rinfo($_POST);
        if($raid)
        {
            admin_ok_page("修改成功,请不要刷新此页面以免出现剑类专精效果.", "./?admin&raidid=$raid[r_id]");
        }
        else
        {
            admin_ok_page("新增成功,请不要刷新此页面以免出现剑类专精效果.", "./?admin/members&raidid=$id");
        }

    }

    include template('admin/raid_info');
}


if($action=='members')
{
    if(!$raid[r_id])admin_err_page('未有对应活动');
    $joined = array();
    $sql = "select * from raid_mdata where raid_id='$raid[r_id]'";
    $rs = $db1->query($sql);
    while($row = $rs->fetch())
    {
        $joined[] = $row['player_id'];
    }

    if($_SERVER['REQUEST_METHOD']=='POST')
    {
        if(!$members)$members = array();
        //$db1->exec("delete from raid_mdata where raid_id='$raid[r_id]]'");
        #for()
        //print_r($members);
        //print_r($_POST['members']);
        $Raid->changeMembers($_POST['members']);
        goURL("./?admin/ev/&raidid=$raid[r_id]");
    }

    $sql = "select m_id,m_name,m_class from players where m_close=0 order by m_class, m_name";
    $rs = $db1->query($sql);
    //$n = 1;
    while($row = $rs->fetch())
    {
        if(in_array($row['m_id'],$joined))$row['checked'] = 'checked';
        //if($n%2==0)$row['css'] = 'odd';
        $players[$row[m_class]][] = $row;
        $n+=1;
    }

    include template('admin/raid_players');
}

if($action=='ev')
{
    $opt_event = array();
    $rs = $db1->query("select * from event_type");
    while($row = $rs->fetch())
    {
        $opt_event[$row[t_name]] = $row[t_name];
        $events[] = $row;
    }

    $memberids = $Raid->memidArray();
    $cond = implode("','", $memberids);
    $sql = "select m_id,m_name from players where m_id in ('$cond') order by m_name";
    $rs = $db1->query($sql);
    $opt_players = array();
    while($row = $rs->fetch())
    {
        $opt_players[$row[m_id]] = $row['m_name'];

    }

    if($_SERVER['REQUEST_METHOD']=='POST')
    {
        if(!$raid)goURL('./?admin');
        $validate = array(
            'dt' => array('type'=>'string', 'min_length'=>10),
            'raid_id'=> array('type'=>'number'),
            'type'  => array('type'=>'string'),
	);
	$result = Validate::multiple($_POST, $validate);
        if(in_array(false, $result))admin_err_page('事件信息不完整,请重新填写');
        
        $post = array(
            'player_id' => $player_id,
            'raid_id' => $raid_id,
            'dt' => $dt,
            'type' => $type,
            'dkp' => $dkp,
            'info' => $info,
        );
        if(!event_filter($post))admin_err_page('请选择事件类型');
        if($re_id)$Raid->modifyEvent(event_filter($post), $re_id);
        else $Raid->addEvent(event_filter($post));
        goURL("./?admin/ev&raidid=$raid[r_id]");
    }
    if($evid)
    {
        $ev = $db1->query("select * from events_raid where re_id='$evid'")->fetch();
    }

    if(!$ev['dt'])$ev['dt'] = $raid['r_start_dt'];
    include template('raid_ev');
}

if($action == 'delev')
{
    $Raid->delEvent($evid);
    goURL("./?admin/ev&raidid=$raid[r_id]");
}

?>